﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace IPSwitcher
{
    public interface IIPSwitcher
    {
        void SwitchIP();
    }

    public class NetGearIPSwitcher : IIPSwitcher
    {
        public void SwitchIP()
        {
            while (true)
            {
                HttpWebRequest request = WebRequest.Create("http://192.168.1.1/apply.cgi?/RST_conn_status.htm") as HttpWebRequest;
                request.AllowAutoRedirect = false;
                request.Method = "POST";
                request.Headers.Add("Authorization", "Basic YWRtaW46cGFzc3dvcmQ=");
                request.ContentType = "application/x-www-form-urlencoded";

                byte[] bytes = Encoding.UTF8.GetBytes("submit_flag=connect_status&endis_connect=3");
                request.ContentLength = bytes.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                HttpWebResponse response = null;
                try
                {
                    response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
        }
    }
}
